/**
 * @author luther.seven@gmail.com
 */

function showUserPopup()
{
    var dctk = connected.toolkit;
    
    // create a new panel for the popup
    var popup = dctk.createPanelCtrl("popup", true);
    popup.width = 240;
    popup.height = 80;
    popup.outlined = true;
    popup.filled = true;
    popup.color.rgb = 0x000000;
    popup.color.a = 192;
	popup.border.thickness = 2.5;
    popup.border.rounded = true;
    popup.onUnhandledKeyEvent = popupButtonHandler;
    
    var zipCodeTxt = dctk.createLabelCtrl("zipCodeTxt", true);
    zipCodeTxt.setRect(0, 50, 82, 70);
    zipCodeTxt.textColor.rgb = 0xCDCDCD;
    zipCodeTxt.fontSize = 18;
	zipCodeTxt.align = dctkLabel.TS_RIGHT;
	zipCodeTxt.valign = dctkLabel.TS_MIDDLE;
    zipCodeTxt.text = "City Code:";
    popup.addChild(zipCodeTxt);
	
	var keyboardTxt = dctk.createLabelCtrl("keyboardTxt", true);
    keyboardTxt.setRect(0, 28, 240, 48);
    keyboardTxt.textColor.rgb = 0xCDCDCD;
    keyboardTxt.fontSize = 14;
	keyboardTxt.align = dctkLabel.TS_CENTER;
	keyboardTxt.valign = dctkLabel.TS_MIDDLE;
    keyboardTxt.text = "Input your 5-digit City Code...";
    popup.addChild(keyboardTxt);

    var zipCodeEdit = dctk.createEditCtrl("zipCodeEdit", true);
    zipCodeEdit.setRect(90, 50, 225, 70);
    zipCodeEdit.text = zipCode;
    zipCodeEdit.fontSize = 20;
    zipCodeEdit.numeric = true;
    popup.addChild(zipCodeEdit);

	var usBtn = dctk.createButtonCtrl("usBtn", true);
    usBtn.setRect(15, 10, 115, 25);
    usBtn.text = "US ZIP";
    usBtn.fontSize = 16;
    usBtn.onSelect = function(e) {  fetchNewSearchResults(); }
    popup.addChild(usBtn);
	
	var intBtn = dctk.createButtonCtrl("intBtn", true);
    intBtn.setRect(125, 10, 225, 25);
    intBtn.text = "Int'l Code";
    intBtn.fontSize = 16;
    intBtn.onSelect = function(e) {  fetchIntlSearchResults(); }
    popup.addChild(intBtn);
	    
    // show the popup
    dctk.invokePopup(popup);
    
	// set siblings
	zipCodeEdit.bottomSibling = usBtn;
	usBtn.topSibling = zipCodeEdit;
	usBtn.rightSibling = intBtn;
	intBtn.leftSibling = usBtn;
	intBtn.topSibling = zipCodeEdit;
	
	dctk.focusedElement = zipCodeEdit;
}

function popupButtonHandler(e)
{
    var dctk = connected.toolkit;

    switch(e.buttonId)
    {
        case dcKeyEvent.BUTTON_ID_BACK:
        {
            dctk.dismissPopup();
        }
        break;

        case dcKeyEvent.BUTTON_ID_SELECT:
        {
            fetchNewSearchResults();
            dctk.dismissPopup();
        }
        break;
    }
}

function fetchNewSearchResults()
{
    var dctk = connected.toolkit;
    var zipCodeEdit = dctk.getElementById("zipCodeEdit");   
    
    zipCode = "zipCode="+zipCodeEdit.text+"&unittype=0";
	var storage = connected.openStorage(weatherStorage,true)
	storage.set(weatherCode,zipCode)
	unit = " F";
	dctk.dismissPopup();
	onLoadComplete();
}

function fetchIntlSearchResults()
{
    var dctk = connected.toolkit;
    var zipCodeEdit = dctk.getElementById("zipCodeEdit");   
    
    zipCode = "cityCode="+zipCodeEdit.text+"&unittype=1";
	var storage = connected.openStorage(weatherStorage,true)
	storage.set(weatherCode,zipCode)
	unit = " C";
	dctk.dismissPopup();
	onLoadComplete();
}

